package com.ramos.kuaixiao.utils;

import android.content.Context;
import android.os.Environment;
import android.telephony.TelephonyManager;

import com.ramos.kuaixiao.App;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public class FileUtil {
	private static final int fileMaxSize = 1024 * 1024;

	public static String getImageZipPath(Context context) {

		StringBuilder sb = new StringBuilder();
		sb.append(getSDPath());
		sb.append(File.separator);
		sb.append(context.getPackageName());
		sb.append(File.separator);
		sb.append("downloadImg/file");

		File path = new File(sb.toString());
		if (!path.exists()) {
			path.mkdirs();
		}
		return sb.toString();
	}

	public static String getImagePath(Context context) {

		StringBuilder sb = new StringBuilder();
		sb.append(getSDPath());
		sb.append(File.separator);
		sb.append(context.getPackageName());
		sb.append(File.separator);
		sb.append("downloadImg/cache");

		File path = new File(sb.toString());
		if (!path.exists()) {
			path.mkdirs();
		}
		return sb.toString();
	}

	public static String getApkPath(Context context) {

		StringBuilder sb = new StringBuilder();
		sb.append(getSDPath());
		sb.append(File.separator);
		sb.append(context.getPackageName());
		sb.append(File.separator);
		sb.append("apk");

		File path = new File(sb.toString());
		if (!path.exists()) {
			path.mkdirs();
		}
		return sb.toString();
	}

	public static String getDataPath(Context context) {

		StringBuilder sb = new StringBuilder();
		sb.append(getSDPath());
		sb.append(File.separator);
		sb.append(context.getPackageName());
		sb.append(File.separator);
		sb.append("Data/dbinfo");

		File path = new File(sb.toString());
		if (!path.exists()) {
			path.mkdirs();
		}
		return sb.toString();
	}

	public static String getZipPath(Context context) {

		StringBuilder sb = new StringBuilder();
		sb.append(getSDPath());
		sb.append(File.separator);
		sb.append(context.getPackageName());
		sb.append(File.separator);
		sb.append("Data/dbinfo");

		File path = new File(sb.toString());
		if (!path.exists()) {
			path.mkdirs();
		}
		return sb.toString();
	}

	public static String getLogPath(Context context) {

		StringBuilder sb = new StringBuilder();
		sb.append(getSDPath());
		sb.append(File.separator);
		sb.append(context.getPackageName());
		sb.append(File.separator);
		sb.append("images/");

		File path = new File(sb.toString());
		if (!path.exists()) {
			path.mkdirs();
		}
		return sb.toString();
	}

	//获取异常日志保存路径。如果不存在，则创建
	public static String getCirclePath(Context context) {

		StringBuilder sb = new StringBuilder();
		sb.append(getSDPath());
		sb.append(File.separator);
		sb.append(context.getPackageName());
		sb.append(File.separator);
		sb.append("images/");

		File path = new File(sb.toString());
		if (!path.exists()) {
			path.mkdirs();
		}
		return sb.toString();
	}

	//获取SD卡路径
	private static String getSDPath() {

		File sdDir = null;
		boolean sdCardExist = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
		if (sdCardExist) {
			sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
		}

		if (null == sdDir) {
			return "";
		} else {
			return sdDir.toString();
		}
	}

	public static String getLineNum(Context mContext){
		try {
			String phoneNumber1 = "";
			TelephonyManager tm = (TelephonyManager) mContext.getSystemService(mContext.TELEPHONY_SERVICE);
			phoneNumber1 = tm.getLine1Number();
			return phoneNumber1;
		}catch (Exception e){
			e.printStackTrace();
			return "";
		}
	}

	protected static void saveCrashLog(String strContent) {
		try {
			File logFile = getLogFile();
			if ((logFile.exists()) && (logFile.length() < fileMaxSize)) {
				StringBuilder sb = new StringBuilder();
				sb.append(DateUtil.dateToString(new Date()));
				sb.append(":");
				sb.append(strContent);
				sb.append("\r\n");
				FileOutputStream fos = new FileOutputStream(logFile, true);
				fos.write(sb.toString().getBytes());
				fos.close();
			}
		} catch (Exception e) {
			CommUtil.d(App.mContext, "exception", CommUtil.getStackTrace(e));
		}
	}

	private static File getLogFile() throws IOException {
		String fileName = saveLogFile();
		File file = new File(fileName);
		if (!file.exists()) {
			file.createNewFile();
		}
		return file;
	}

	private static String saveLogFile() {
		StringBuilder sb = new StringBuilder();
		sb.append(getLogPath(App.mContext));
		sb.append(File.separator);
		sb.append(DateUtil.dateToString(new Date()));
		sb.append("_exception");
		sb.append(".log");
		return sb.toString();
	}
}
